/*****************************************************************************************
  Project   : The Impact of Unions on Non-union Wage Setting: Threats and Bargaining
  Authors   : David A. Green, Ben M. Sand, Iain G. Snoddy, Jeanne Tschopp
  Date      : August 2025
  Output    : Data for Figure 2 -- transition to union jobs probabilities
  Notes     : This script should be run after main data cleaning and variable construction
*****************************************************************************************/

// transition probabilities to union jobs

// transitions data 
use ${wd}/ipums_ind_trans/transitions_all_case_0.dta, clear

egen n_to_u = sum(phi_ii) if union==0 & union_2==1 & job != job_2, by(year job )

egen u_to_u = sum(phi_ii) if union==1 & union_2==1 & job != job_2, by(year job )

egen n_to_n = sum(phi_ii) if union==0 & union_2==0 & job != job_2, by(year job )

egen u_to_n = sum(phi_ii) if union==1 & union_2==0 & job != job_2, by(year job )

egen same_n = sum(phi_ii) if union==0 & job == job_2 , by(year job )

egen same_u = sum(phi_ii) if union==1 & job == job_2 , by(year job )

collapse (mean) n_to_u u_to_u n_to_n u_to_n same_n same_u wgt_1 union [aw=1], by(year job)

gen test1 = n_to_u + n_to_n + same_n
gen test2 = u_to_u + u_to_n + same_u

collapse (mean) n_to_u u_to_u  n_to_n u_to_n  same_n same_u [aw=wgt_1], by(year )


gen     time = "1980" if year2 == 1
replace time = "1990" if year2 == 2
replace time = "2000" if year2 == 3
replace time = "2010" if year2 == 4
replace time = "2020" if year2 == 5
drop year2
order time

lab var n_to_u "Non Union-to-Union"
lab var u_to_u "Union-to-Union"
lab var n_to_n "NU-to-NU"
lab var u_to_n "U-to-NU"
lab var same_n "same job, non-union"
lab var same_u "same job, union"
lab var time "Year"

format *to* same_* %9.3f
order time n_* same_n u_*
keep time n_to_u u_to_u

save ${wd}/Transition_to_union.dta, replace	
 
